# Produce table summaries of the models in the main text.
# See Appendix M.




#############
# load data #
#############

library(brms)
library(ggplot2)
library(ggpubr)
library(stargazer)

load("PSRM Replication Files/PresidentialElectionsPosts.RData")

colnames(president_df)

length(unique(president_df$country)) # 26
length(unique(president_df$electionname)) # 29
length(unique(president_df$party_country)) # 52

# focus on 15 days before and after the election
day_range <- 15

sub <- president_df[which(president_df$daysinceelection >= -1*day_range
                          & president_df$daysinceelection <= day_range),]

# winners and losers
winners <- sub[which(sub$win == 1),]
losers <- sub[which(sub$win != 1),]

length(unique(winners$electionname[which(!is.na(winners$polarization))])) # 27
length(unique(winners$electionname[which(!is.na(winners$economic_polarization))])) # 27
length(unique(winners$electionname[which(!is.na(winners$social_polarization))])) # 27
length(unique(winners$electionname[which(!is.na(winners$populist_present))])) # 27
length(unique(winners$electionname[which(!is.na(winners$populist_dummy))])) # 27

length(unique(losers$electionname[which(!is.na(losers$polarization))])) # 27
length(unique(losers$electionname[which(!is.na(losers$economic_polarization))])) # 27
length(unique(losers$electionname[which(!is.na(losers$social_polarization))])) # 27
length(unique(losers$electionname[which(!is.na(losers$populist_present))])) # 27
length(unique(losers$electionname[which(!is.na(losers$populist_dummy))])) # 27




####################
# regression table #
####################

load("PSRM Replication Files/Stan_15Days.RData")

summary(winner_love1)
summary(winner_angry1)
summary(loser_love1)
summary(loser_angry1)

round(posterior_interval(winner_angry1, "post", 0.9), 2)
round(posterior_interval(loser_angry1, "post", 0.9), 2)

round(mean(winner_love1$data[which(winner_love1$data[,2] == 0),1]), 2)
round(mean(winner_angry1$data[which(winner_love1$data[,2] == 0),1]), 2)
round(mean(loser_angry1$data[which(loser_angry1$data[,2] == 0),1]), 2)

# priors
winner_love1[[3]]
winner_love2[[3]]

winner_angry1[[3]]
winner_angry2[[3]]

loser_love1[[3]]
loser_love2[[3]]

loser_angry1[[3]]
loser_angry2[[3]]


# table
f1 <- lm(loveprop ~ post
         + populist_present + polarization
         + incumbentparty + concurrent + runoff + semipresidential
         + Xl + Xr,
         data=winners)

f2 <- lm(loveprop ~ post*populist_present + post*polarization
         + incumbentparty + concurrent + runoff + semipresidential
         + Xl + Xr,
         data=winners)

f3 <- lm(angryprop ~ post
         + populist_present + polarization
         + incumbentparty + concurrent + runoff + semipresidential
         + Xl + Xr,
         data=winners)

f4 <- lm(angryprop ~ post*populist_present + post*polarization
         + incumbentparty + concurrent + runoff + semipresidential
         + Xl + Xr,
         data=winners)

f5 <- lm(loveprop ~ post
         + populist_present + polarization
         + incumbentparty + concurrent + runoff + semipresidential
         + Xl + Xr,
         data=losers)

f6 <- lm(loveprop ~ post*populist_present + post*polarization
         + incumbentparty + concurrent + runoff + semipresidential
         + Xl + Xr,
         data=losers)

f7 <- lm(angryprop ~ post
         + populist_present + polarization
         + incumbentparty + concurrent + runoff + semipresidential
         + Xl + Xr,
         data=losers)

f8 <- lm(angryprop ~ post*populist_present + post*polarization
         + incumbentparty + concurrent + runoff + semipresidential
         + Xl + Xr,
         data=losers)


# table M.1
stargazer(f1, f3, f5, f7,
          coef=list(fixef(winner_love1)[,1], fixef(winner_angry1)[,1], 
                    fixef(loser_love1)[,1], fixef(loser_angry1)[,1]),
          #se=list(fixef(winner_love1)[,2], fixef(winner_angry1)[,2], 
          #        fixef(loser_love1)[,2], fixef(loser_angry1)[,2]),
          ci.custom=list(fixef(winner_love1)[,3:4], fixef(winner_angry1)[,3:4], 
                         fixef(loser_love1)[,3:4], fixef(loser_angry1)[,3:4]),
          omit="Constant",
          star.cutoffs=NA, 
          digits=2,
          no.space=TRUE,
          dep.var.caption="",
          omit.stat = c("f", "adj.rsq", "rsq", "ser"),
          dep.var.labels=c("Love", "Angry", "Love", "Angry"),
          covariate.labels=c("Post Election",
                             "Populist Involvement", "Polarization",
                             "Incumbent Party", "Concurrent Election",
                             "Runoff", "Semi-Presidential",
                             "Effective Number of Candidates",
                             "Pre-Election Trend (Group Mean)", 
                             "Post-Election Trend (Group Mean)"))

# standard deviation of random effects
getSigmas <- function(model){
  sest <- round(summary(model)[[17]]$party_election[1:3,1], 2)
  ssd <- paste0("[",
                sapply(1:3, function(p){paste(round(summary(model)[[17]]$party_election[p,3:4], 2), 
                                              collapse = ", ")}), "]")
  
  return(c(sest[1], ssd[1], sest[2], ssd[2], sest[3], ssd[3]))
}

sigmas1 <- data.frame(label=c("hatsigmatextIntercept", "",
                              "hatsigmatextPre-Election Trend", "",
                              "hatsigmatextPost-Election Trend", ""),
                      s1=getSigmas(winner_love1),
                      s2=getSigmas(winner_angry1),
                      s3=getSigmas(loser_love1),
                      s4=getSigmas(loser_angry1))
stargazer(t(t(sigmas1)))

c(length(unique(winner_love1$data$party_election)),
  length(unique(winner_angry1$data$party_election)),
  length(unique(loser_love1$data$party_election)),
  length(unique(loser_angry1$data$party_election)))


# table M.2
stargazer(f2, f4, f6, f8,
          coef=list(fixef(winner_love2)[,1], fixef(winner_angry2)[,1], 
                    fixef(loser_love2)[,1], fixef(loser_angry2)[,1]),
          ci.custom=list(fixef(winner_love2)[,3:4], fixef(winner_angry2)[,3:4], 
                         fixef(loser_love2)[,3:4], fixef(loser_angry2)[,3:4]),
          omit="Constant",
          star.cutoffs=NA, 
          digits=2,
          no.space=TRUE,
          dep.var.caption="",
          omit.stat = c("f", "adj.rsq", "rsq", "ser"),
          dep.var.labels=c("Love", "Angry", "Love", "Angry"),
          covariate.labels=c("Post Election",
                             "Populist Involvement", "Polarization",
                             "Incumbent Party", "Concurrent Election",
                             "Runoff", "Semi-Presidential",
                             "Effective Number of Candidates",
                             "Pre-Election Trend (Group Mean)", 
                             "Post-Election Trend (Group Mean)",
                             "Post Election times Populist Involvement",
                             "Post Election times Polarization"))

# standard deviation of random effects
sigmas2 <- data.frame(label=c("hatsigmatextIntercept", "",
                              "hatsigmatextPre-Election Trend", "",
                              "hatsigmatextPost-Election Trend", ""),
                      s1=getSigmas(winner_love2),
                      s2=getSigmas(winner_angry2),
                      s3=getSigmas(loser_love2),
                      s4=getSigmas(loser_angry2))
stargazer(t(t(sigmas2)))

c(length(unique(winner_love2$data$party_election)),
  length(unique(winner_angry2$data$party_election)),
  length(unique(loser_love2$data$party_election)),
  length(unique(loser_angry2$data$party_election)))
